package yz.cp.back.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yz.cp.back.auth.security.SecUser;
import yz.cp.back.auth.security.SecurityUtils;
import yz.cp.back.common.base.BaseController;
import yz.cp.back.common.enums.*;
import yz.cp.back.common.res.ApiResult;
import yz.cp.back.common.utils.PageKit;
import yz.cp.back.f.entity.FinanceChargeDraw;
import yz.cp.back.f.service.IFinanceChargeDrawService;
import yz.cp.back.u.entity.UserRole;
import yz.cp.back.u.service.IUserRoleService;
import yz.cp.back.user.service.FinanceAuditManage;
import yz.cp.back.user.service.UserRoleMange;
import yz.cp.back.user.vo.FinanceChargeBo;
import yz.cp.back.user.vo.FinanceQueryVo;

import javax.validation.Valid;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/web/finance")
public class FinanceController extends BaseController {
    public UserRole userRoleConstant = new UserRole();
    @Autowired
    private IFinanceChargeDrawService financeChargeDrawService;
    @Autowired
    private FinanceAuditManage financeAuditManage;
    @Autowired
    private UserRoleMange userRoleMange;
    @Autowired
    private IUserRoleService userRoleService;

    @PostMapping("getByCondition")
    public ApiResult getByCondition(FinanceQueryVo vo) {
        QueryWrapper<FinanceChargeDraw> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(StringUtils.isNotBlank(vo.getMerchantName()), FinanceChargeDraw::getMerchantName, vo.getMerchantName())
                .like(StringUtils.isNotBlank(vo.getUsername()), FinanceChargeDraw::getUserName, vo.getUsername())
                .eq(StringUtils.isNotBlank(vo.getAccessId()), FinanceChargeDraw::getAccessId, vo.getAccessId())
                .eq(vo.getStatus() != null, FinanceChargeDraw::getStatus, vo.getStatus())
                .eq(vo.getRoleType() != null && vo.getRoleType() != -1, FinanceChargeDraw::getRoleType, vo.getRoleType());
        wrapper.lambda().select(FinanceChargeDraw::getId, FinanceChargeDraw::getFlowNo
                , FinanceChargeDraw::getMerchantName, FinanceChargeDraw::getUserName,
                FinanceChargeDraw::getMerchantType, FinanceChargeDraw::getAccessId,
                FinanceChargeDraw::getRoleType, FinanceChargeDraw::getPayeeBaName,
                FinanceChargeDraw::getPayeeBankName, FinanceChargeDraw::getPayeeBaNumber,
                FinanceChargeDraw::getPayerBaName, FinanceChargeDraw::getPayerBankName,
                FinanceChargeDraw::getPayerBaNumber, FinanceChargeDraw::getAmount,
                FinanceChargeDraw::getPayeeBankBranch, FinanceChargeDraw::getPayerBankBranch,
                FinanceChargeDraw::getType, FinanceChargeDraw::getStatus, FinanceChargeDraw::getEaccountType,
                FinanceChargeDraw::getCtime, FinanceChargeDraw::getRby, FinanceChargeDraw::getRtime);
        IPage<FinanceChargeDraw> page = financeChargeDrawService.page(PageKit.buildPage(vo, wrapper), wrapper);
        Map<String, UserRole> userRoleMap = Maps.newHashMap();
        if (page.getRecords() != null && page.getRecords().size() > 0) {
            List<String> roleList = Lists.newArrayList();
            page.getRecords().forEach(v -> {
                if (StringUtils.isNotBlank(v.getAccessId()))
                    roleList.add(v.getAccessId());
            });
            userRoleMange.getSimpleAccessUserRoleById(roleList, userRoleMap);
        }
        page.convert((v) -> {
            Map map = beanMapper.map(v, Map.class);
            map.put("merchantTypeName", MerchantType.getByCode(v.getMerchantType()).getName());
            map.put("accessName", userRoleMap.getOrDefault(v.getAccessId(), userRoleConstant).getMerchantAbbrName());
            map.put("opName", EAccountType.getByCode(v.getEaccountType()).getName() + FinanceOpType.getByCode(v.getType()).getName());
            return map;
        });
        return renderOk().add(page);
    }


    /**
     * 充值或提现审核
     */
    @PostMapping("/audit")
    public ApiResult audit(@Valid FinanceChargeBo bo) {
        SecUser secUser = SecurityUtils.getSecUser();
        return financeAuditManage.audit(bo, secUser);
    }

    @RequestMapping(value = "/detail/{id}", method = RequestMethod.POST)
    public ApiResult detail(@PathVariable String id) {
        FinanceChargeDraw financeChargeDraw = financeChargeDrawService.getById(id);
        if (financeChargeDraw == null) {
            return renderError("找不到详情");
        }
        Map map = beanMapper.map(financeChargeDraw, Map.class);
        UserRole userRole = userRoleService.getById(financeChargeDraw.getAccessId());
        map.put("merchantTypeName", MerchantType.getByCode(financeChargeDraw.getMerchantType()).getName());
        map.put("roleTypeName", RoleType.getByCode(financeChargeDraw.getRoleType()).getName());
        map.put("eaccountTypeName", EAccountType.getByCode(financeChargeDraw.getEaccountType()).getName() + "账户");
        DecimalFormat df = new DecimalFormat("¥ #,##0.00");
        map.put("amountName", df.format(financeChargeDraw.getAmount()));
        map.put("statusName", FinanceStatus.getByCode(financeChargeDraw.getStatus()).getName());
        if (financeChargeDraw.getRamount() != null)
            map.put("ramountName", df.format(financeChargeDraw.getRamount()));
        if (userRole != null) {
            map.put("accessName", userRole.getMerchantAbbrName());
        } else {
            map.put("accessName", "N/A");
        }
        return renderOk().add(map);
    }


}
